package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.entity.DbTombInfo;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


import java.math.BigInteger;

@Cmd(code = GameProtocol.S_TOMB_ITEM_BAG, desc = "玩家物品背包")
public class TombItemBag extends AbstractCommand {

    private Tomb.TombItemBagReq reqMsg;

    private Tomb.TombItemBagResp.Builder respBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = Tomb.TombItemBagReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respBuilder = Tomb.TombItemBagResp.newBuilder();
        this.respBuilder.setRet(0);
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.respBuilder.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_ITEM_BAG, this.respBuilder);
            return;
        }

        int ret = player.getModule(TombModule.class).isOpen(true);
        if (ret > 0) {
            this.respBuilder.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_ITEM_BAG, this.respBuilder);
            return;
        }

        DbTombInfo dbTombInfo = player.getModule(TombModule.class).getDbTombInfo(TombMgr.getActivityInfo().getActivityId());
        Property property = new Property();
        dbTombInfo.getTakeCareItemMap().forEach((itemId, itemNum) -> {
            property.addProperty(itemId, BigInteger.valueOf(itemNum));
        });
        this.respBuilder.setRewards(PropertyHelper.parsePropertyToString(property));

        player.sendPacket(ClientProtocol.U_TOMB_ITEM_BAG, this.respBuilder);

    }

    @Override
    public void sendData(GamePlayer player) {

    }

    @Override
    public boolean isDebug() {
        return true;
    }

    @Override
    public String getRequestData() {
        return this.reqMsg.toString();
    }
}
